package com.kuang.springcloud.controller;

import com.kuang.springcloud.pojo.Dept;
import com.kuang.springcloud.service.DeptService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//提供restful服务
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;


    //@Autowired//DiscoveryClient 导入Spring的包
    // private DiscoveryClient client;
    //Eureka 配置的一些信息
    @Autowired
    private DiscoveryClient client;

    @PostMapping("/dept/add")
    public boolean addDept(Dept dept) {

        return deptService.addDept(dept);
    }

    @GetMapping("/dept/get/{id}")
    public Dept queryById(@PathVariable("id") Long id) {

        return deptService.queryById(id);
    }

    @GetMapping("/dept/list")
    public List<Dept> queryAll() {

        return deptService.queryAll();
    }

    @RequestMapping("/dept/discovery")
    public Object discovery() {
        //获取微服务列表的清单
        List<String> services = client.getServices();
        System.out.println("discovery:" + services);

        //得到一个具体的微服务信息，通过具体的微服务id，也就是appinlicationname
        List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT");//里面的参数serviceId就是eureka页面的名字
        //ServiceInstance  点看进源码一看，里面有主机名称 host 端口 uri什么的
        for (ServiceInstance instance : instances) {
            System.out.println(
                    instance.getHost() + "\t" +
                            instance.getPort() + "\t" +
                            instance.getUri() + "\t" +
                            instance.getServiceId()
            );
        }
        // //结果：192.168.181.1	8001	http://192.168.181.1:8001	SPRINGCLOUD-PROVIDER-DEPT
        return this.client;
//         //这些写完，还要去启动类加一个注解
    }



}
